package city.spring.modules.medicalstation.drugwarehouse.service.impl;

import city.spring.modules.medicalstation.drugwarehouse.entity.DrugwarehouseEntity;
import city.spring.modules.medicalstation.drugwarehouse.repository.DrugwarehouseRepository;
import city.spring.modules.medicalstation.drugwarehouse.service.DrugwarehouseService;
import city.spring.modules.medicalstation.drugwarehouse.vo.DrugwarehouseVO;
import city.spring.modules.medicalstation.drugwarehouseShelfExt.entity.DrugwarehouseShelfExtEntity;
import city.spring.modules.medicalstation.drugwarehouseShelfExt.repository.DrugwarehouseShelfExtRepository;
import city.spring.modules.medicine.entity.MedicinereceiptEntity;
import city.spring.modules.medicine.service.MedicinedeliveryService;
import city.spring.modules.medicine.service.MedicinereceiptService;
import city.spring.modules.medicine.vo.MedicinedeliveryVO;
import city.spring.modules.personnel.entity.EmployeeEntity;
import city.spring.modules.personnel.repository.EmployeeRepository;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：医护站药品入库
 *
 * @author 谢金婷
 * @date 2020-09-21 17:00:56
 */
@CacheConfig(cacheNames = {DrugwarehouseServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DrugwarehouseServiceImpl extends ServiceImpl<DrugwarehouseRepository, DrugwarehouseEntity> implements DrugwarehouseService {

    private final UserService userService;
    private final MedicinereceiptService medicinereceiptService;
    private final EmployeeRepository employeeRepository;
    private final DrugwarehouseShelfExtRepository drugwarehouseShelfExtRepository;
    @Autowired
    private MedicinedeliveryService medicinedeliveryService;

    public DrugwarehouseServiceImpl(UserService userService, MedicinereceiptService medicinereceiptService, EmployeeRepository employeeRepository, DrugwarehouseShelfExtRepository drugwarehouseShelfExtRepository) {
        this.userService = userService;
        this.medicinereceiptService = medicinereceiptService;
        this.employeeRepository = employeeRepository;
        this.drugwarehouseShelfExtRepository = drugwarehouseShelfExtRepository;
    }

    @Override
    public void saveDrugwarehouse(DrugwarehouseEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDrugwarehouse(DrugwarehouseEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改

        // 如果当前登录用户为部门领导则可以审核
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setReviewer(userEntity.getId());
        //审核时间
        entity.setApprovalsDate(new Date());

        if (entity.getWarehousingmethod().equals("本人缴存") || entity.getWarehousingmethod().equals("家属缴存")) {
            EmployeeEntity duties = employeeRepository.getDuties("1275357230287912962");
            if (userEntity.getId().equals(duties.getId())) {
                updateById(entity);
            } else {
                throw new RuntimeException("无法保存，您没有审核权限");
            }
        } else {
            MedicinedeliveryVO medicinedelivery = medicinedeliveryService.getMedicinedeliveryById(String.valueOf(entity.getDeliveryId()));
            MedicinereceiptEntity medicinereceipt = medicinereceiptService.getMedicinereceiptById(String.valueOf(medicinedelivery.getMedicinereceiptId()));
            EmployeeEntity duties = employeeRepository.getDuties(medicinereceipt.getDepartment());
            if (userEntity.getId().equals(duties.getId())) {
                updateById(entity);
            } else {
                throw new RuntimeException("无法保存，您没有审核权限");
            }

        }


//        if (!updateById(entity)) {
//            throw new RuntimeException("修改信息失败");
//        }
    }

    @Override
    public void deleteDrugwarehouse(Integer primaryKey) {
        removeById(primaryKey);
        List<DrugwarehouseShelfExtEntity> medicinewarehouseShelfs = drugwarehouseShelfExtRepository.getByWarehousingId(primaryKey);
        System.out.println("medicinewarehouseShelfs" + medicinewarehouseShelfs);
        if (!medicinewarehouseShelfs.isEmpty()) {
            List<String> medicinewarehousingShelfId = medicinewarehouseShelfs.stream().map(DrugwarehouseShelfExtEntity::getId).collect(Collectors.toList());
            drugwarehouseShelfExtRepository.deleteBatchIds(medicinewarehousingShelfId);
        }
    }

    @Override
    public void deleteDrugwarehouse(List<Integer> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<DrugwarehouseVO> listPage(Page<? extends DrugwarehouseEntity> page, Wrapper<? extends DrugwarehouseEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public IPage<DrugwarehouseVO> otherlistPage(Page<? extends DrugwarehouseEntity> page, Wrapper<? extends DrugwarehouseEntity> wrapper) {
        return baseMapper.otherlistPage(page, wrapper);
    }
}